﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB55GZ12Data : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";
        DataTable tbAB55GZ12OUTPUT;
        public int ThreadCount;
        public AB55GZ12Data()
            : base()
        {
        }
        public AB55GZ12Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();

            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region

                or1.CommandText = "SELECT Distinct ''AS LSNAME1,ABPAUFKRS ,ABPAUFLFD,ABPAUFPOS, "
                    + " ABPAUFKRS ||'-'||TO_NUMBER(ABPAUFLFD)||'-'||TO_NUMBER(ABPAUFPOS) AS WEAVINGORDER, ABPSTARTDAT, "
                    + " TRIM(ABPART)||'-'||TRIM(ABPFARBE)||'-'||TRIM(ABPVAR) AS ARTICLE, "
                    + " ABPSOLLMENGE1/100 AS GREIGEQTY,TRUNC(ABPSOLLMENGE1/100/WAKEAFAKTR*1000)AS QTY,0 AS WARPQTY, "
                    + " TRIM(GAART)||'/'||TRIM(GAVAR)||'/'||TRIM(GAFARBE) AS YARN, "
                    + " TRIM(GSGARNBEZ) AS DES,'' WARPINGORDER,GAKG5/100 AS DNQTY, "
                    + " GSCBEZ AS COLOR,GAKG1/100 AS REQUIREMENT,GAKG3/100 AS ALLOCATION,(GAKG5-GAKG6)/100 AS REMAIN "
                    + " FROM INTEX.ABPOS,INTEX.WASTKE,INTEX.LIEFDAT,INTEX.GAAUF,INTEX.GSDAT,INTEX.GSCOLOR  "
                    + " WHERE  ABPMANDANT = WAKMANDANT AND ABPART = WAKART "
                    + " AND TRIM(ABPVAR) = TRIM(WAKVAR) AND ABPFARBE = WAKWCOL  "
                    + " AND ABPMANDANT = GAMANDANT AND ABPAUFKRS = GAAUFKRS "
                    + " AND ABPAUFLFD = GAAUFLFD AND ABPAUFPOS = GAAUFPOS AND GAMANDANT = GSMANDANT "
                    + " AND GAART = GSART  AND GAVAR = GSVAR AND GAMANDANT = GSCMANDANT(+)  "
                    + " AND GAART = GSCART(+) AND GAVAR = GSCVAR(+)  AND GAFARBE = GSCFARBE(+) "
                    + " AND  GASA in ('732','733') AND GADISPOKZ = 'B' AND ABPAUFKRS LIKE 'W%' "
                    + " AND ABPERLKZ=' ' AND ABPSTARTDAT >'" + WBSRTDATE + "' AND ABPSTARTDAT<='" + WBENTDATE + "' "
                    + " ORDER BY 1,2,3,4";
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "AB55GZ12";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    foreach (DataRow dw in setLocal.Tables["AB55GZ12"].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["DES"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["DES"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["COLOR"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["COLOR"] = value;
                    }
                }
                foreach (DataTable dtTemp in setLocal.Tables)
                {
                    DataSet setUI = new DataSet();
                    setUI = ImportDataTable(setUI, dtTemp);
                    DataTable tableABPOS = setUI.Tables["AB55GZ12"];
                    foreach (DataRow dwTmp in tableABPOS.Rows)
                    {
                        setUI = GetABAUFM(setUI, dwTmp);
                        setUI = GetABFOLGE(setUI, dwTmp);
                    }
                    foreach (DataRow rowABPOS in setUI.Tables["AB55GZ12"].Rows)
                    {
                        double WARPQTY = System.Convert.ToDouble(rowABPOS["WARPQTY"].ToString());
                        double QTY = System.Convert.ToDouble(rowABPOS["QTY"].ToString());
                        double REQUIREMENT = System.Convert.ToDouble(rowABPOS["REQUIREMENT"].ToString());

                        REQUIREMENT = WARPQTY / QTY * REQUIREMENT;
                        rowABPOS["REQUIREMENT"] = System.Math.Round(REQUIREMENT, 2);
                    }
                    if (setLocal.Tables.Contains("AB55GZ12"))
                    {
                        setLocal.Tables["AB55GZ12"].Clear();
                    }
                    setLocal = ImportDataTable(setLocal, setUI.Tables["AB55GZ12"]);
                }
                
                    setLocal.AcceptChanges();

                    set4Return.Merge(setLocal);
                                   
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet GetABAUFM(DataSet dsTarget, DataRow dwABPOS)
        {

            string TableName = "ABAUFM";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT distinct ABAAUFKRS||'-'||TO_NUMBER(ABAAUFLFD)||'-'||TO_NUMBER(ABAAUFPOS) AS WARPINGORDER, "
                    + " ABASANZSTK*ABASSTKLG/1000 as WARPQTY "
                    + " FROM ABAUFM,ABPOS  WHERE ABAAUFKRS = ABPAUFKRS AND ABAAUFLFD = ABPAUFLFD  "
                    + " AND ABAAUFPOS = ABPAUFPOS AND ABAAPOS >'0001' AND ABPAUFART = 'VW' "
                    + " AND  ABAVKAUFKRS = :ABAVKAUFKRS AND ABAVKAUFLFD=:ABAVKAUFLFD AND ABAVKAUFPOS=:ABAVKAUFPOS";
                orlReadCommand.Parameters.Add("ABAVKAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                orlReadCommand.Parameters.Add("ABAVKAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                orlReadCommand.Parameters.Add("ABAVKAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABAUFM";
                    string orderno = "";
                    double WARPQTY = 0;
                    foreach (DataRow dwTemp in dsResult.Tables["ABAUFM"].Rows)
                    {
                        orderno += dwTemp["WARPINGORDER"].ToString() + ";";
                        WARPQTY += System.Convert.ToDouble(dwTemp["WARPQTY"].ToString());
                    }
                    dwABPOS["WARPINGORDER"] = orderno;
                    dwABPOS["WARPQTY"] = WARPQTY;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;

        }
        public DataSet GetABFOLGE(DataSet dsTarget, DataRow dwABPOS)
        {
            string TableName = "ABFOLGE";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT DISTINCT B.LSNAME1 FROM ABFOLGE A,LIEFDAT B "
                    + " WHERE A.ABFMANDANT = B.LSMANDANT AND A.ABFLIEFNR = B.LSLIEFNR "
                    + " AND ABFMANUELKZ = 'N' AND ABFMANDANT = '00' AND ABFAUFKRS = :ABFAUFKRS  "
                    + " AND ABFAUFLFD = :ABFAUFLFD  AND ABFAUFPOS = :ABFAUFPOS  "
                     ;
                orlReadCommand.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString().Trim();
                orlReadCommand.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString().Trim();
                orlReadCommand.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString().Trim();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;

                    byte[] b;
                    string value;
                    string ColumnName = "";
                    foreach (DataRow dw in dsResult.Tables["ABFOLGE"].Rows)
                    {
                        ColumnName = "LSNAME1";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;

                        dwABPOS["LSNAME1"] = dw["LSNAME1"];
                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public DataSet BuildGAAUF(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}